package com.deyuanyun.pic.ctrl.controller.vo;

import com.alibaba.fastjson.annotation.JSONField;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.ctrl.domain.CpProtectionParam;
import com.deyuanyun.pic.ctrl.enumeration.PlanEndMode;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.Date;

/**
 *  阴保设施保护参数扩展类，方便前台封装参数
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-09-10
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
public class CpProtectionParamExpandVO extends CpProtectionParam implements Serializable {

    private static final long serialVersionUID = -3478787049655895166L;

    private String frequency; //检测频率

    private String applyTo;//适用（场站阀室：VALVEROOM、管线：PIPELINE）

    private String pipeLineId; //管道id

    private String fromPointId;// 起点距离编号

    private String toPointId;//终点距离编号

    private String result;//检测周期

    private Date realityBeginDate; //计划实际开始时间

    public String getFrequency() {
        return frequency;
    }

    public void setFrequency(String frequency) {
        this.frequency = frequency;
    }

    public String getApplyTo() {
        return applyTo;
    }

    public void setApplyTo(String applyTo) {
        this.applyTo = applyTo;
    }

    public Date getRealityBeginDate() {
        return super.getBeginPlanDate() != null ? super.getBeginPlanDate() : realityBeginDate;
    }

    public void setRealityBeginDate(Date realityBeginDate) {
        this.realityBeginDate = realityBeginDate;
    }

    public String getPipeLineId() {
        return pipeLineId;
    }

    public void setPipeLineId(String pipeLineId) {
        this.pipeLineId = pipeLineId;
    }

    public String getToPointId() {
        return toPointId;
    }

    public void setToPointId(String toPointId) {
        this.toPointId = toPointId;
    }

    public String getFromPointId() {
        return fromPointId;
    }

    public void setFromPointId(String fromPointId) {
        this.fromPointId = fromPointId;
    }

    /**
     * 返回检测周期文本信息
     * @return
     */
    public String getResult() {
        if(getBeginPlanDate() != null && StringUtils.isNotEmpty(getEndMode())
                && StringUtils.isNotEmpty(getAlertMode()) && getAlertAdvanceCount() != null
                && StringUtils.isNotEmpty(getAlertAdvanceTime()))
            return getSaveProParamResult().getResult();
        return result;
    }

    private String resultStr = "{0}开始，{1}结束";

    /**
     * 返回检测周期
     * @return
     */
    @JSONField(serialize = false)
    public ProtectionParamDateVO getSaveProParamResult() {

        if(this.getRealityBeginDate() != null) {
            ProtectionParamDateVO protectionParamDateVO = new ProtectionParamDateVO();

            protectionParamDateVO.setDeviceObject(getDeviceObject());
            protectionParamDateVO.setDeviceParam(getDeviceParam());

            String endMode = getEndMode(); //结束方式
            Date endDate = getEndDate();//结束时间
            Integer endCount = getEndCount();//结束次数

            LocalDateTime beginDateJoda = LocalDateTime.fromDateFields(this.getRealityBeginDate());

            PlanEndMode planEndMode = PlanEndMode.valueOf(endMode);
            if (PlanEndMode.COUNT.equals(planEndMode)) { //指定次数结束
                protectionParamDateVO.setResult(MessageFormat.format(resultStr, beginDateJoda.toString(StringUtils.DATE), "重复" + endCount + "次"));
            } else if (PlanEndMode.DATE.equals(planEndMode)) { //指定结束时间
                protectionParamDateVO.setResult(MessageFormat.format(resultStr, beginDateJoda.toString(StringUtils.DATE),
                        LocalDate.fromDateFields(endDate).toString("yyyy-MM-dd")));
            } else { //从不结束
                protectionParamDateVO.setResult(MessageFormat.format(resultStr, beginDateJoda.toString(StringUtils.DATE), "从不"));
            }
            return protectionParamDateVO;
        }
        return null;
    }

    public void setResult(String result) {
        this.result = result;
    }
}
